<template>
  <div>
    <el-form class="registerContainer"
             ref="registerForm"
             :model="registionForm"
             :rules="rules"
             v-loading="loading"
             element-loading-text="拼命加载中"
             element-loading-spinner="el-icon-loading"
             element-loading-background="rgba(0, 0, 0, 0.8)">
      <h3 class="loginTitle">用户注册</h3>
      <el-button type="text" @click="toUserLogin">返回</el-button>
      <el-form-item label="用户名" prop="username">
        <el-input type="text" v-model="registionForm.username" auto-complete="false"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="registionForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width: 100%" @click="register">注册</el-button>

      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Registion",
  data() {
    return {
      registionForm:{
        username: '',
        password: ''
      },
      rules:{
        username: [
          { required: true, message: '请输入注册的账号', trigger: 'blur' },
          { min: 6, max: 13, message: '长度在 6 到 13 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入注册密码', trigger: 'blur' },
          { min: 6, max: 13, message: '长度在 6 到 13 个字符', trigger: 'blur' }
        ]
      },
      loading:false
    }
  },
  methods: {
    toUserLogin() {
      this.$router.push("/userLogin")
    },

    register(){
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.postRequest(this.$store.state.baseUrl+'/user/registerUser',{
            username:this.registionForm.username,
            password:this.registionForm.password
          }).then(resp => {
            this.$message({
              type:'success',
              message:'注册成功'
            });
            this.$router.replace('/userLogin');
          })
        } else {
          this.$message({
            type:'error',
            message:"异常原因注册失败，请联系管理员"
          })
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.registerContainer {
  background-clip: padding-box;
  border-radius: 15px;
  margin: 180px auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  box-shadow: 0 0 25px #c8c9cc;
  border: 1px solid #8cc5ff;

}

.loginTitle{
  text-align: center;
  font-size: 20px;
  user-select: none;
}

</style>
